<template>
	<!-- 20230201 用于我的订单列表 我的订单详情 底部的操作按钮 -->
	<view class="mall-my-order-btn-cn">
		<!-- 组件内容 -->
		<view class="cn-content">
			<view class="flex align-center justify-end">
				<!-- 待支付 -->
				<block v-if="status == 1">
					<view class="btn-60-cc" @click="cancel()">取消订单</view>
					<view class="btn-60-main" @click="isPayLayerShow = true">支付
					</view>
				</block>

				<!-- 待发货 -->
				<block v-if="status==2">
					<view class="btn-60-cc" @click="remind()">提醒发货</view>
				</block>

				<!-- 待收货 -->
				<block v-if="status==3">
					<view class="btn-60-cc" @click="go('/pages/mall/logistics?id='+orderId)">查看物流</view>
					<view class="btn-60-main" @click="finish()">确认收货</view>
				</block>

				<!-- 待评价 -->
				<block v-if="status==4">
					<view class="btn-60-cc" @click="del()">删除订单</view>
					<view class="btn-60-cc" @click="go('/pages/mall/logistics?id='+orderId)">查看物流</view>
					<view class="btn-60-main" @click="go('/pages/mall/my/evaluate?id='+orderId)">去评价</view>
				</block>

				<!-- 已完成 -->
				<block v-if="status==5">
					<view class="btn-60-cc" @click="del()">删除订单</view>
					<view class="btn-60-cc" @click="go('/pages/mall/logistics?id='+orderId)">查看物流</view>
					<view class="btn-60-main" v-if="isEvaluateAdd==0"
						@click="go('/pages/mall/my/evaluateAdd?id='+orderId)">
						追加评价
					</view>
				</block>

				<!-- 退款中 -->
				<block v-if="status==6">
					<!-- <view class="btn">退款进度</view> -->
				</block>

				<!-- 退款成功 -->
				<block v-if="status==7">
					<view class="btn-60-cc" @click="del()">删除订单</view>
				</block>

				<!-- 已取消 -->
				<block v-if="status==8">
					<view class="btn-60-cc" @click="del()">删除订单</view>
					<view class="btn-60-main" @click="reBuy()">再次购买</view>
				</block>
			</view>
		</view>

		<!-- 组件浮层 -->
		<view class="cn-layer">
			<!-- 订单支付方式 -->
			<ty-layer-pay :show.sync="isPayLayerShow" :money="myMoney" :pay-money="payMoney" :pay-way="payWay"
				@pay="pay">
			</ty-layer-pay>
		</view>
	</view>
</template>

<script>
	import mall from "../api/mall.js"
	export default {
		props: {
			// 订单ID
			orderId: {
				type: [Number, String],
				default: ''
			},
			// 当前index
			index: {
				type: [Number, String],
				default: ''
			},
			// 当前状态 1--待支付 2--待发货 3--待收货 4--待评价 5--已完成 6--退款中 7--退款成功 8-已取消
			status: {
				type: [Number, String],
				default: ''
			},
			// 支付金额
			payMoney: {
				type: [Number, String],
				default: ''
			},
			// 我的余额
			myMoney: {
				type: [Number, String],
				default: 0
			},
			// 是否有追评 0--没有 1--有
			isEvaluateAdd: {
				type: [Number, String],
				default: 0
			},
			// 类型 1--订单列表页 2-订单详情页
			type: {
				type: [Number, String],
				default: 1
			},
		},
		data() {
			return {
				// 支付浮层是否显示
				isPayLayerShow: false,
				// 订单支付方式
				payWay: ['weixin', 'alipay', 'money']
				// payMoney: 0,
				// index: 0, //当前点击第X个
			}
		},


		methods: {
			/**
			 * 订单取消
			 * @date 2023-02-01
			 */
			cancel() {
				this.cn.confirm('确认取消订单').then(res => {
					if (res.confirm) {
						mall.order.cancelOrder(this.orderId).then(res => {
							if (res.code == 0) {
								this.cn.toast(res.msg)
								if (this.type == 1) {
									uni.redirectTo({
										url: '/pages/mall/my/orderList'
									});
								} else if (this.type == 2) {
									uni.redirectTo({
										url: '/pages/mall/my/orderDetail?id=' + this.orderId
									})
								}
							}
						})
					}
				})
			},

			/**
			 * 订单支付
			 * @param {Object} e
			 * @date 2023-02-01
			 */
			pay(e) {
				let payType = e.pay_type
				this.cn.tip.showLoading()
				mall.order.rePayOrder(this.orderId, payType).then(res => {
					if (res.code == 0) {
						// 关闭支付浮层
						this.isPayLayerShow = false
						// 微信支付或支付宝支付
						if (payType == 1 || payType == 2) {
							// H5支付宝支付
							if (payType == 2 && this.cn.userAgent() == 'h5') {
								this.$emit('h5AliPay', {
									rawHtml: res.data.pay_params
								})
								// H5微信支付
							} else if (payType == 1 && this.cn.userAgent() == 'h5') {
								// 记录已跳转状态
								uni.setStorageSync('hasOpenMwebUrl', 1)
								// 跳转H5支付
								window.location.href = res.data.pay_params
								// 其他非H5平台
							} else {
								this.cn.requestPayment(res.data.pay_params, payType).then(res => {
									if (this.type == 1) {
										uni.redirectTo({
											url: '/pages/mall/my/orderList'
										});
									} else if (this.type == 2) {
										uni.redirectTo({
											url: '/pages/mall/my/orderDetail?id=' + this.orderId
										});
									}
								}).catch(res => {
									if (this.type == 1) {
										uni.redirectTo({
											url: '/pages/mall/my/orderList'
										});
									} else if (this.type == 2) {
										uni.redirectTo({
											url: '/pages/mall/my/orderDetail?id=' + this.orderId
										});
									}
								})
							}
							// 余额支付
						} else {
							this.cn.alert(res.msg).then(res => {
								if (this.type == 1) {
									uni.redirectTo({
										url: '/pages/mall/my/orderList'
									});
								} else if (this.type == 2) {
									uni.redirectTo({
										url: '/pages/mall/my/orderDetail?id=' + this.orderId
									});
								}
							})
						}
					} else {
						this.cn.tip.hideLoading()
					}
				})
			},

			/**
			 * 提醒发货
			 * @date 2023-02-01
			 */
			remind() {
				mall.order.remindOrder(this.orderId).then(res => {
					if (res.code == 0) {
						this.cn.toast(res.msg)
					}
				})
			},

			/**
			 * 确认收货
			 * @date 2023-02-01
			 */
			finish() {
				this.cn.confirm('确认收货吗').then(res => {
					if (res.confirm) {
						mall.order.finishOrder(this.orderId).then(res => {
							if (res.code == 0) {
								this.cn.toast(res.msg)
								if (this.type == 1) {
									uni.redirectTo({
										url: '/pages/mall/my/orderList'
									});
								} else if (this.type == 2) {
									uni.redirectTo({
										url: '/pages/mall/my/orderDetail?id=' + this.orderId
									});
								}
							}
						})
					}
				})
			},

			/*
			 * 订单删除
			 * @date 2023-02-02
			 */
			del() {
				this.cn.confirm('确认删除订单').then(res => {
					if (res.confirm) {
						mall.order.deleteOrder(this.orderId).then(res => {
							if (res.code == 0) {
								this.cn.toast(res.msg);
								if (this.type == 1) {
									this.$emit('del', {
										// 打开类型 1--订单列表页 2-订单详情页
										type: this.type,
										// 当前index
										index: this.index,
										// 订单ID
										order_id: this.orderId
									})
								} else {
									uni.redirectTo({
										url: '/pages/mall/my/orderList'
									});
								}
							}
						})
					}
				})
			},

			/*
			 * 再次购买
			 * @date 2023-02-02
			 */
			reBuy() {
				mall.order.reBuyOrder(this.orderId).then(res => {
					if (res.code == 0) {
						this.cn.alert(res.msg).then(() => {
							uni.switchTab({
								url: '/pages/mall/cart'
							})
						})
					}
				})
			}
		}
	}
</script>

<style scoped>

</style>